<?php

namespace App\Http\Controllers\Api;

use App\Models\Hero;
use App\Services\AppointService;
use App\Services\ConfigService;
use App\Services\PositionService;
use Dingo\Api\Http\Request;

class InitController extends BaseController
{
    public function switch() {
        $cur_time = time();
        $start_time = ConfigService::getValue('START_DATE');
        $end_time = ConfigService::getValue('END_DATE');
        $status = 0;
        if ($cur_time >= strtotime($start_time) && $cur_time <= strtotime($end_time)) {
            $status = 1;
        }
        return $this->success($status);
    }

    public function param(Request $request)
    {
        $date = \request('date', date('Y-m-d'));
        $time = \request('time', 1);

        $arr = [];
        $arr['start_date'] = ConfigService::getValue('SELECT_START_DATE');
        $arr['end_date'] = ConfigService::getValue('SELECT_END_DATE');
        //时间段
        $arr['time'] = ConfigService::getAll();
//        $arr['position'] = PositionService::get();

        //剩余数
        if ($date == '2020-04-04') {
            $arr['left_num'] = 0;
        } else {
            $arr['left_num'] = AppointService::getLeftNum($date, $time);
        }

        $user = $this->getUser();
        if ($user) {
            $arr['order'] = AppointService::order($user->id);
            $arr['has_order'] = $arr['order'] != null;
        }
        return $this->success($arr);
    }

    public function clearCache(Request $request) {
        $is_all = $request->input('is_all', 0);
        if ($is_all) {
            Hero::flushCache();
            Hero::flushTags();
        }
        return ConfigService::forget();
    }

    public function initCache() {
        Hero::init();
    }

    public function test(Request $request) {
        $type = $request->input('type');
        $key = $request->input('key');
        if ($type == 'hero_all') {
            return Hero::getInfo($key);
        }
    }

}